﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Rendering;
using System;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Collections;
using Ecset = Unity.Entities.Entity;
using Ecsmgr = Unity.Entities.EntityManager;

public enum VoxelType
{
    metal = 2,
    glass = 3,
    wood = 4,
    soil = 7,
    ice = 10,
}


public class Voxel
{
    public string name;
    public int id;
    public Func<Vector2, VoxelComponentData> creator;    
}

public static class VoxelStatic
{
    /*public const float VOXELSIZE = 0.5f;
    public const float HALFVOXELSIZE = 0.25f;*/

    private const int TYPEAMOUNT = 16;

    public static Voxel[] voxelValues;

    public static EntityArchetype voxelArchetype = new EntityArchetype();

    public static Ecset[] voxProto = new Ecset[TYPEAMOUNT];

    

    //public static void InitWorld()
    //public static void Init(Ecsmgr mgr)
    //{
    //    voxelValues = new Voxel[TYPEAMOUNT];
    //    voxelValues[(int)VoxelType.Void] = new Voxel()
    //    {
    //        id = (int)VoxelType.Void,   
    //    };
    //    voxelValues[(int)VoxelType.Soil] = new Voxel()
    //    {
    //        id = (int)VoxelType.Soil,   
    //        name = "Soil",
    //        creator = (coord) => new VoxelComponentData { coordWorld = coord, blood = 100, defence = 10, sharpness = 10, magicDefence = 10 , elem = (int)VoxelType.Soil},
    //    };
    //    voxelValues[(int)VoxelType.Wood] = new Voxel()
    //    {
    //        id = (int)VoxelType.Wood,
    //        name = "Wood",
    //        creator = (coord) => new VoxelComponentData { coordWorld = coord, blood = 100, defence = 10, sharpness = 10, magicDefence = 10, elem = (int)VoxelType.Wood},
    //    };

    //    for(int i=1;i<TYPEAMOUNT;i++)
    //    {
    //        GetEntityProto(voxelValues[i], mgr);
    //    }
    //    Debug.Log("END INIT");
    //}

    public static void Init(Ecsmgr mgr)
    {
        voxelValues = new Voxel[TYPEAMOUNT];

        voxelValues[(int)VoxelType.metal] = new Voxel()
        {
            name = "Metal",
            id = 2,
            creator = (coord) => new VoxelComponentData { coordWorld = coord, blood = 100, defence = 10, sharpness = 10, magicDefence = 10, mass = 1, elem = (int)ElementState.metal },
        };
        voxelValues[(int)VoxelType.glass] = new Voxel()
        {
            name = "Glass",
            id = 3,
            creator = (coord) => new VoxelComponentData { coordWorld = coord, blood = 100, defence = 10, sharpness = 10, magicDefence = 10, mass = 1, elem = (int)ElementState.glass },
        };
        voxelValues[(int)VoxelType.wood] = new Voxel()
        {
            name = "Wood",
            id = 4,
            creator = (coord) => new VoxelComponentData { coordWorld = coord, blood = 100, defence = 10, sharpness = 10, magicDefence = 10, mass = 1, elem = (int)ElementState.wood },
        };
        voxelValues[(int)VoxelType.soil] = new Voxel()
        {
            name = "Soil",
            id = 7,
            creator = (coord) => new VoxelComponentData { coordWorld = coord, blood = 100, defence = 10, sharpness = 10, magicDefence = 10, mass = 1, elem = (int)ElementState.soil },
        };
        voxelValues[(int)VoxelType.ice] = new Voxel()
        {
            name = "Ice",
            id = 10,
            creator = (coord) => new VoxelComponentData { coordWorld = coord, blood = 100, defence = 10, sharpness = 10, magicDefence = 10, mass = 1, elem = (int)ElementState.ice },
        };

        for (int i = 1; i < TYPEAMOUNT; i++)
        {
            if(voxelValues[i] != null)
                GetEntityProto(voxelValues[i], mgr);
        }
        Debug.Log("END INIT");
    }

    static void GetEntityProto(Voxel v, Ecsmgr mgr)
    {
        BlobAssetStore blobAssetStore = new BlobAssetStore();
        var proto = Resources.Load("Terrian/" + v.name) as GameObject;
        if(proto == null)
        {
            Debug.LogWarning("未找到" + v.name + "的GO");
            return;
        }
        var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, blobAssetStore);
        voxProto[v.id] = GameObjectConversionUtility.ConvertGameObjectHierarchy(proto, settings);
        voxProto[v.id] = mgr.Instantiate(voxProto[v.id]);
        mgr.SetEnabled(voxProto[v.id], false);
        voxelArchetype = mgr.CreateArchetype(mgr.GetComponentTypes(voxProto[v.id]).ToArray());
        blobAssetStore.Dispose();
    }

    /// <summary>
    /// 根据世界坐标生成
    /// </summary>
    /// <param name="mgr"></param>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public static Ecset Spawn(Ecsmgr mgr, float x, float y, Voxel v)
    {
        var e = mgr.Instantiate(voxProto[v.id]);
        mgr.SetName(e, "Riko");
        mgr.SetComponentData<VoxelComponentData>(e, voxelValues[v.id].creator(new Vector2(x, y)));

        mgr.SetComponentData<Translation>(e, new Translation { Value = new float3(x ,y ,0) });
        mgr.SetEnabled(e, true);
        return e;
    }

}



